package com.example.rubbish.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class HDataBase {
//    private int type=TYPE_NULL;
//    public static final int TYPE_NULL=-1;
//    public static final int TYPE_HISTORY=0;
//    public static final int TYPE_INFO=1;
    private static  final String HISTORY_DB_NAME="History.db";

    private static final int VERSION = 1;
    private static HDataBase HDataBase;
    private static SQLiteDatabase db;


    private HDataBase(Context context) {

            DataBaseHelper dataBaseHelper = new DataBaseHelper(context,HISTORY_DB_NAME,null, VERSION);
            db=dataBaseHelper.getWritableDatabase();
    }

    public synchronized static HDataBase getInstance(Context context) {
        if(HDataBase ==null){
            HDataBase =new HDataBase(context);
        }

        return HDataBase;
    }

    //save history
    public  void saveHistory(History history){
        if(history!=null) {
            ContentValues values = new ContentValues();
            values.put("history_name",history.getHistoryName());
            db.insert("history",null,values);
        }
    }
    public void delHistory(String content){
        db.delete("history", "history_name = ?", new String[] {content});
    }
    public void delAllHistory(){

        db.delete("history", null, null);
    }

    //load history
    public List<History> loadHistory(){
        List<History> list =new ArrayList<>();

        Cursor cursor = db.query("history",
                null,
                null,
                null,
                null,
                null,
                null);

        if(cursor.moveToFirst()){
            do{
                History history = new History();
                int i_id=cursor.getColumnIndex( "id");

                int i_history_name= cursor.getColumnIndex("history_name");
                history.setId(cursor.getInt(i_id));
                history.setHistoryName(cursor.getString(i_history_name));
                list.add(history);
            }while(cursor.moveToNext());
        }
        if(cursor!=null) {
            cursor.close();
        }
        return list;
    }//loadHistory()






}
